package com.g4.shoppingsingle.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.g4.shoppingsingle.model.DdUsersInfo;
import com.g4.shoppingsingle.model.Permission;
import com.g4.shoppingsingle.model.Role;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2021-10-27
 */
@Repository
public interface DdUsersInfoMapper extends BaseMapper<DdUsersInfo> {
//    @Select("SELECT user_name,PASSWORD from dd_users_info")
//    List<DdUsersInfo> getMessage();
////    List<DdUsersInfo> users(String user_name,String password);

    @Select("SELECT p.id, p.name\n" +
            "FROM dd_users_info u\n" +
            "LEFT JOIN user_role ur ON u.id=ur.user_id\n" +
            "LEFT JOIN role r       ON r.id=ur.role_id\n" +
            "LEFT JOIN role_permission rp ON r.id=rp.role_id\n" +
            "LEFT JOIN permission p ON p.id=rp.permission_id\n" +
            "WHERE u.id=#{id}")
    List<Permission> findUserPermissionsById(Integer id);

    //根据手机号查询用户对象
    @Select("select * from dd_users_info where telephone=#{telephone}")
    DdUsersInfo findUserByTelephone(String telephone);

    //根据用户名查询用户对象
    @Select("select * from dd_users_info where user_name=#{username}")
    DdUsersInfo findUserByUserName(String username);

    // 根据用户id查询用户角色
    @Select("SELECT r.id, r.name \n" +
            "FROM dd_users_info u\n" +
            "LEFT JOIN user_role ur ON u.id=ur.user_id\n" +
            "LEFT JOIN role r       ON r.id=ur.role_id\n" +
            "WHERE u.id=#{id}")
    List<Role> findUserRolesById(Integer id);
}



